package com.lgl.study.resume.controller;

import com.lgl.study.resume.dao.IResumeDao;
import com.lgl.study.resume.pojo.Resume;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Optional;

/**
 * @Auther: Brook
 * @Date: 2020/10/13 22:22
 * @Description:
 */
@Controller
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    private IResumeDao resumeDao;

    @RequestMapping("/findList")
    public String findList(Model model) {

        List<Resume> resumes = resumeDao.findAll();
        model.addAttribute("resumes", resumes);
        return "resumeList";
    }

    @RequestMapping("/resumeEdit")
    public String resumeEdit(Long id, Model model) {
        Resume resume = new Resume();
        if (id != null) {
            Optional<Resume> optionalResume = resumeDao.findById(id);
            resume = optionalResume.get();
        }
        model.addAttribute("resume", resume);
        return "resumeEdit";
    }

    @RequestMapping("/resumeDelete")
    public String resumeDelete(Long id) {
        resumeDao.deleteById(id);
        return "redirect:/resume/findList";
    }

    @RequestMapping("/resumeUpdate")
    public String resumeUpdate(Resume resume) {
        Resume result = resumeDao.saveAndFlush(resume);
        System.out.println("完成更新：" + result);
        return "redirect:/resume/findList";
    }
}
